package com.sense.kukoo.common.wiget;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;

import com.sense.kukoo.common.R;
import com.sense.kukoo.common.utils.ApplicationContext;
import com.sense.kukoo.lib_logger.LoggerManager;

public class KukooSearchView extends SearchView {

    private final String TAG = KukooSearchView.class.getSimpleName();

    private EditText searchEdit;
    private View search_plate;
    private View submit_area;
    private ImageView search_button;
    //关闭图标
    private ImageView search_close_btn;
    //搜索图标
    private ImageView search_mag_icon;

    public KukooSearchView(@NonNull Context context) {
        super(context);
        init();
    }

    public KukooSearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public KukooSearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        searchEdit = findViewById(androidx.appcompat.R.id.search_src_text);
        search_plate = findViewById(androidx.appcompat.R.id.search_plate);
        submit_area = findViewById(androidx.appcompat.R.id.submit_area);

        search_button = findViewById(androidx.appcompat.R.id.search_button);
        search_close_btn = findViewById(androidx.appcompat.R.id.search_close_btn);
        search_mag_icon = findViewById(androidx.appcompat.R.id.search_mag_icon);
        //隐藏下划线
        search_plate.setBackground(null);
        submit_area.setBackground(null);

        //设置边距
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) search_mag_icon.getLayoutParams();
        LoggerManager.d(TAG, "layoutParams.getMarginStart()="+layoutParams.getMarginStart());
        LoggerManager.d(TAG, "layoutParams.getMarginEnd()="+layoutParams.getMarginEnd());
        LoggerManager.d(TAG, "layoutParams.leftMargin()="+layoutParams.leftMargin);
        LoggerManager.d(TAG, "layoutParams.rightMargin()="+layoutParams.rightMargin);
        LoggerManager.d(TAG, "layoutParams.width="+layoutParams.width);
        LoggerManager.d(TAG, "layoutParams.height="+layoutParams.height);
        layoutParams.setMarginStart(0);
        layoutParams.leftMargin = 0;
        layoutParams.width = ApplicationContext.dp2px(20);
        layoutParams.height = ApplicationContext.dp2px(20);
        search_mag_icon.setLayoutParams(layoutParams);

        //search_mag_icon.setBackgroundColor(R.color.common_bg_edit_select_on);
        //search_close_btn.setBackgroundColor(R.color.common_bg_edit_select_on);

        LoggerManager.d(TAG, "getPaddingLeft="+getPaddingLeft());
        LoggerManager.d(TAG, "getPaddingRight="+getPaddingRight());
        LoggerManager.d(TAG, "getPaddingStart="+getPaddingStart());
        LoggerManager.d(TAG, "getPaddingEnd="+getPaddingEnd());

        //todo 设置光标
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            searchEdit.setTextCursorDrawable(com.sense.kukoo.common_design.R.drawable.design_cursor);
        }
    }

    public EditText getEditText(){
        return searchEdit;
    }

}
